package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.MainActivity;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.BuyerMoneyInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.entity.bean.BankInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;
import com.tiza.xiaoyoubao.utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/4/4.
 * 佣金提现
 */

public class CommissionDepositFragment extends BaseFragment {
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    @BindView(R.id.tv_bankDetial)
    TextView tv_bankDetial;
    //查看明细
    @BindView(R.id.tv_checkDetial)
    TextView tv_checkDetial;
    //提现金额
    @BindView(R.id.tv_DepositMoney)
    TextView tv_DepositMoney;
    //登录密码
    @BindView(R.id.et_passWord)
    EditText et_passWord;
    @BindView(R.id.view_commission)
    View view_commission;
    @BindView(R.id.view_principal)
    View view_principal;
    private Bundle args;
    private BankInfo bankInfo;
    private String payMoney, payUser, payBank, payTime, payCard, yue, passWord, passWord2;
    private String payFlag = "";
    private String checkTime = "";
    private String payFlagStr = "";
    private String state;
    private SimpleDateFormat df;
    private int money;
    private String depositType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        args = getArguments();
        bankInfo = (BankInfo) args.getSerializable("info");
        state = args.getString("state");
        df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_commission_deposit;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        tv_bankDetial.setText("银行：" + bankInfo.getBankName() + " " + bankInfo.getHolder().substring(0, 1) + "**" + " " + bankInfo.getAccount().substring(0, 4) + "***" + bankInfo.getAccount().substring(bankInfo.getAccount().length() - 4, bankInfo.getAccount().length()));
        if(state.equals("commission")){
            view_commission.setVisibility(View.VISIBLE);
            money=30;
            depositType="yj";
        }else if(state.equals("principal")){
            view_principal.setVisibility(View.VISIBLE);
            money=10;
            depositType="bj";
        }
        getBuyerMoney();
    }

    @OnClick({R.id.tv_checkDetial, R.id.btn_Ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_checkDetial:
                Bundle args=new Bundle();
                args.putString("state",state);
                UIHelper.showCommissionDepositDetial(mActivity,args);
                break;
            case R.id.btn_Ok:
                String message = check();
                if (TextUtils.isEmpty(message)) {
                    setBankInfo();
                } else {
                    toast(message);
                }
                break;
            default:
                break;
        }
    }

    public String check() {
        passWord = et_passWord.getText().toString().trim();
        passWord2 = Hawk.get(Constants.KEY_USER_PASSWD, "");
        if (!passWord.equals(passWord2))
            return "请输入正确的登录密码";
        payMoney = tv_DepositMoney.getText().toString().trim();
        if (Double.parseDouble(payMoney) - money < 0)
            return "您所提现的余额不足"+money+"元";
        return "";
    }

    private void setBankInfo() {
        payTime = df.format(new Date());
        payCard = bankInfo.getAccount();
        payBank = bankInfo.getBankName();
        payUser = bankInfo.getHolder();
        ApiHttpClient.commissionDeposit(CommissionDepositFragment.this, userInfo.getWebzzCmsBuyerInfo().getUserid(), payMoney, payUser, payBank, payTime, payCard, yue, depositType,
                new JsonCallback<String>(String.class, true) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("提现失败，请重试");
                    }

                    @Override
                    public void onResponse(ResponseInfo<String> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        toast("提现成功!");
                        UIHelper.show(getActivity(), MainActivity.class, true);
                        finish();
                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                    }
                }
        );
    }


    private void getBuyerMoney() {
        ApiHttpClient.getBuyerMoney(this, userInfo.getWebzzCmsBuyerInfo().getUserid(),
                new JsonCallback<BuyerMoneyInfo>(BuyerMoneyInfo.class) {

                    @Override
                    public void onBefore(Request request) {
                        super.onBefore(request);
                        showWaitingDialog();
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        toast("获取信息失败");
                    }

                    @Override
                    public void onResponse(ResponseInfo<BuyerMoneyInfo> response) {
                        int code = response.getCode();
                        String message = response.getMessage();
                        if (code == 1) {
                            showWarnDialog(message);
                            return;
                        }
                        List<BuyerMoneyInfo> data = response.getData();
                        if (null == data || data.size() == 0) {
                            toast("获取信息失败");
                            return;
                        }
                        BuyerMoneyInfo moneyInfo = data.get(0);
                        if(moneyInfo != null){
                            if (state.equals("principal")) {
                                    yue = DateUtils.formateRate(moneyInfo.getBalance());
                            } else if (state.equals("commission")) {
                                    yue = DateUtils.formateRate(moneyInfo.getCmi());
                            }
                            payMoney = DateUtils.formateRate(yue);
                            tv_DepositMoney.setText(DateUtils.formateRate(yue));
                        }else{
                            tv_DepositMoney.setText("0.00");
                        }

                    }

                    @Override
                    public void onAfter() {
                        super.onAfter();
                        hideWaitingDialog();
                    }
                }
        );
    }
}
